﻿@model ProductModel

@if (!String.IsNullOrEmpty(Model.Id))
{

    <div class="panel panel-default">
        <vc:admin-widget widget-zone="product_details_cross_sells_top" additional-data="Model" />
        <div class="panel-heading">
            @T("Admin.Catalog.Products.CrossSells")
        </div>
        <div class="panel-body">
            <div id="crosssellproducts-grid"></div>
        </div>
        <div class="panel-footer">
            <button type="submit" id="btnAddNewCrossSellProduct" onclick="javascript:OpenWindow('@(Url.Action("CrossSellProductAddPopup", "Product", new { productId = Model.Id, btnId = "btnRefreshCrossSellProducts", formId = "product-form" }))    ', 800, 800, true); return false;" class="k-button"><i class="fa fa-plus"></i>&nbsp;@T("Admin.Catalog.Products.CrossSells.AddNew")</button>
            <input type="submit" id="btnRefreshCrossSellProducts" style="display: none" />
        </div>
        <vc:admin-widget widget-zone="product_details_cross_sells_bottom" additional-data="Model" />
    </div>

    <script>
        $(document).ready(function () {
            $("#crosssellproducts-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("CrossSellProductList", "Product", new { productId = Model.Id }))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        },
                        destroy: {
                            url: "@Html.Raw(Url.Action("CrossSellProductDelete", "Product", new { productId = Model.Id }))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                ProductId: { editable: false, type: "string" },
                                Product2Name: { editable: false, type: "string" },
                            }
                        }
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    numeric: false,
                    previousNext: false,
                    info: false
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [
                    {
                        field: "Product2Name",
                        title: "@T("Admin.Catalog.Products.CrossSells.Fields.Product")",
                        template: '<a class="k-link" href="@Url.Content("~/Admin/Product/Edit/")#=Id#">#=kendo.htmlEncode(Product2Name)#</a>',
                    }, {
                        command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                        title: "@T("Admin.Common.Delete")",
                    }
                ]
            });
        });
    </script>
            
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnRefreshCrossSellProducts').click(function () {
                //refresh grid
                var grid = $("#crosssellproducts-grid").data('kendoGrid');
                grid.dataSource.read();

                //return false to don't reload a page
                return false;
            });
        });
    </script>

}
else
{
    <div class="note note-info">
        @T("Admin.Catalog.Products.CrossSells.SaveBeforeEdit")
    </div>
}